<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/Public/static/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/Public/static/css/public.css" media="all"/>
    <style>
        body{overflow-y: scroll;}
    </style>
</head>
<body class="childrenBody">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline" style="width: 15%">
                    <input type="search" name="keyword" autocomplete="off" class="layui-input" placeholder="规则值/名"/>
                </div>
                <div class="layui-input-inline" style="width: 10%">
                    <select name="status">
                        <option value="">状态</option>
                        <option value="1">使用</option>
                        <option value='0'>停用</option>
                    </select>
                </div>
                <div class="layui-input-inline" style="width: 10%">
                    <select name="is_url">
                        <option value="">链接</option>
                        <option value="1">是</option>
                        <option value='0'>否</option>
                    </select>
                </div>
                <div class="layui-input-inline" style="width: 15%">
                    <select name="type">
                        <option value="">支持表达式</option>
                        <option value="1">是</option>
                        <option value='0'>否</option>
                    </select>
                </div>
                <div class="layui-input-inline" style="width: 15%">
                    <select name="pid">
                        <option value="">所属父级</option>
                        <option value="0">顶级权限</option>
                        <volist name="list" id="v">
                            <option value="{$v.id}">{$v._name}</option>
                        </volist>
                    </select>
                </div>
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-green layui-btn-sm search_btn" title="搜索">
                        <i class="layui-icon layui-icon-search "></i>
                    </a>
                    <a class="layui-btn layui-btn-normal layui-btn-sm add_btn" title="添加">
                        <i class="layui-icon layui-icon-add-circle"></i>
                    </a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm delAll_btn" title="批量删除">
                        <i class="layui-icon layui-icon-delete"></i>
                    </a>
                    <a class="layui-btn layui-btn-sm" title="刷新当前页" href="javascript:void(0);"
                            onclick="layer.load(1);window.location.reload(true);"><i class="layui-icon">&#xe669;</i></a>
                </div>
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-primary layui-btn-sm showAll" title="展开"><i class="layui-icon layui-icon-triangle-r"></i>
                        展开
                    </a>
                </div>
            </div>
        </form>
    </blockquote>
    <table id="table" lay-filter="table"></table>
    <!--支持表达式状态-->
    <script type="text/html" id="type">
        <input type="checkbox" name="type" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="type" {{ d.type == 1 ? 'checked' : '' }}>
    </script>
    <!--使用状态-->
    <script type="text/html" id="status">
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="正常|停用" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
    </script>
    <!--是否链接-->
    <script type="text/html" id="is_url">
        <input type="checkbox" name="is_url" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="is_url" {{ d.is_url == 1 ? 'checked' : '' }}>
    </script>
    <!--操作-->
    <script type="text/html" id="tool">
        <div class="layui-btn-group">
            <a class="layui-btn layui-btn-xs layui-btn-green" lay-event="add" title="添加子规则">
                <i class="layui-icon layui-icon-add-circle"></i>
            </a>
        <a class="layui-btn layui-btn-xs" lay-event="edit" title="编辑">
            <i class="layui-icon layui-icon-edit"></i>
        </a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" title="删除">
            <i class="layui-icon layui-icon-delete"></i>
        </a>
        </div>
    </script>
<script type="text/javascript" src="/Public/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/Public/static/layui/layui.js"></script>
<script type="text/javascript">
    layui.config({
        base: '/Public/static/layui/lay/modules/'
    }).extend({
        treetable: 'treetable-lay/treetable'
    });
    var tablist = {};
    layui.use(['form', 'layer', 'laydate', 'table', 'laytpl','treetable'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            treetable = layui.treetable,
            table = layui.table;
        //
        tablist.reload = function(where){
            let load = layer.msg('数据加载中',{icon:16,time:false});
            treetable.render({
                treeColIndex: 2,          // 显示在第几列
                treeSpid: 0,             // 顶级pid
                treeIdName: 'id',       // id
                treePidName: 'pid',     // pid
                treeDefaultClose: true,   // 不全部展开
                treeLinkage: true,        //  父级展开时是否自动展开所有子级
                elem: '#table',
                where: where,
                url: '/admin/auth/rule',
                method: 'get',
                cellMinWidth: 95,
                height: "full - 10",
                id: "table",
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'id', title: 'ID', width: 60, align: "center"},
                    {field: 'title', title: '规则名',width:240},
                    {field: 'name', title: '规则值'},
                    {field: 'type',unresize:true,width:100, title: '支持表达式', align: 'center', templet: "#type"},
                    {field: 'is_url', width:80, title: '链接', align: 'center', templet: "#is_url",unresize:true},
                    {field: 'status',unresize:true,width:95, title: '状态', align: 'center', templet: "#status"},
                    {field: 'condition', title: '表达式'},
                    {title: '操作', width: 140, templet: '#tool',align: "center"}
                ]],
                done:function(){
                    layer.close(load);
                }
            });
        };
        tablist.reload();
        //检测更改支持表达式
        form.on('switch(type)', function (obj) {
            var _this = $(obj.elem);
            var index = layer.msg('操作中', {'icon': 16, shade: 0.1});
            $.post("/admin/auth/fast_change_rule", {
                id: this.value,
                name: this.name,
                val: obj.elem.checked
            }, function (data) {
                layer.close(index);
                if (data.code === 0) {
                    window.parent.toast('操作成功', 0);
                } else {
                    !obj.elem.checked ? _this.prop('checked',true) : _this.removeAttr('checked');
                    form.render('checkbox');
                    window.parent.toast(data.info, data.code);
                    //状态回滚
                }
            });
        });
        //检测更改使用状态
        form.on('switch(status)', function (obj) {
            var _this = $(obj.elem);
            var index = layer.msg('操作中', {'icon': 16, shade: 0.1});
            $.post("/admin/auth/fast_change_rule", {
                id: this.value,
                name: this.name,
                val: obj.elem.checked
            }, function (data) {
                layer.close(index);
                if (data.code === 0) {
                    window.parent.toast('操作成功', 0);
                } else {
                    !obj.elem.checked ? _this.prop('checked',true) : _this.removeAttr('checked');
                    form.render('checkbox');
                    window.parent.toast(data.info, data.code);
                    //状态回滚
                }
            });
        });
        //检测更改是否链接
        form.on('switch(is_url)', function (obj) {
            var _this = $(obj.elem);
            var index = layer.msg('操作中', {'icon': 16, shade: 0.1});
            $.post("/admin/auth/fast_change_rule", {
                id: this.value,
                name: this.name,
                val: obj.elem.checked
            }, function (data) {
                layer.close(index);
                if (data.code === 0) {
                    window.parent.toast('操作成功', 0);
                } else {
                    !obj.elem.checked ? _this.prop('checked',true) : _this.removeAttr('checked');
                    form.render('checkbox');
                    window.parent.toast(data.info, data.code);
                    //状态回滚
                }
            });
        });
        //搜索
        $(".search_btn").on("click", function () {
            search();
        });
        //搜索+刷新
        function search(){
            tablist.reload({
                    keyword:$("input[name=keyword]").val()
                    ,type:$('select[name=type]').val()
                    ,status:$('select[name=status]').val()
                    ,is_url:$('select[name=is_url]').val()
                    ,pid:$('select[name=pid]').val()
            });
        }

        //添加文章
        function addoredit(url,name) {
            let index = layui.layer.open({
                title: name,
                type: 2,
                content: url,
                maxmin:true,
                area:['60%','98%']
            });
        }

        $(".add_btn").on('click',function () {
            addoredit('/admin/auth/add_rule','添加规则');
        });

        //展开
        $(".showAll").toggle(function(){
            $(this).html('<i class="layui-icon layui-icon-triangle-d"></i>收起');
            treetable.expandAll('#table');
        },function(){
            $(this).html('<i class="layui-icon layui-icon-triangle-r"></i>展开');
            treetable.foldAll('#table');
        });
        //批量删除
        $(".delAll_btn").on('click',function () {
            var checkStatus = table.checkStatus('table'),
                data = checkStatus.data,
                ids = [];
            if (data.length > 0) {
                for (var i in data) {
                    ids.push(data[i].id);
                }
                layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                    $.post("/admin/auth/del_rule",{'id':ids},function(data){
                        window.parent.toast(data.info,data.code);
                        if(data.code === 0)
                        tablist.reload();
                    });
                    layer.close(index);
                })
            } else {
                layer.msg("请选择需要删除的数据");
            }
        });

        //列表操作
        table.on('tool(table)', function (obj) {
            var layEvent = obj.event,
                data = obj.data;
            if (layEvent === 'edit') { //编辑
                addoredit('/admin/auth/edit_rule/id/'+data.id,'修改 <span class="layui-red"> '+data.title+' </span>');
            } else if (layEvent === 'del') { //删除
                layer.confirm('确定删除 <span class="layui-red">' + data.title + '</span> 规则？', {icon: 3, title: '提示'}, function (index) {
                    layer.close(index);
                    $.post("/admin/auth/del_rule",{'id':data.id},function(data){
                        window.parent.toast(data.info,data.code);
                        if(data.code === 0)
                            tablist.reload();
                    });
                });
            }else if(layEvent === 'add'){
                addoredit('/admin/auth/add_rule/id/'+data.id,'为 <span class="layui-red"> '+data.title+' </span>添加子项');
            }
        });
        if(window.parent.NProgress){
            window.parent.NProgress.done();
        }
    })
</script>
</body>
</html>